public class PrototypeTest {
    public static void main(String[] args) throws CloneNotSupportedException {
        BaseInfo baseInfo = new BaseInfo("aaa");
        Product4 product4 = new Product4("part1", "part2", "part3", 1, 2,baseInfo);
        Product4 clone = product4.clone();
        System.out.println(product4);
        System.out.println(clone);
        //修改引用对象
        baseInfo.setCompanyName("bbb");
        System.out.println(product4);
        System.out.println(clone);

    }
}
//被引用对象
class BaseInfo implements Cloneable{
    private String companyName;
    @Override
    public String toString() {
        return "["+super.hashCode()+"]BaseInfo{" +
                "companyName='" + companyName + '\'' +
                '}';
    }

    @Override
    protected BaseInfo clone() throws CloneNotSupportedException {
        return ((BaseInfo) super.clone());
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public BaseInfo(String companyName) {
        this.companyName = companyName;
    }
}
class Product4 implements Cloneable{
    private String part1;
    private String part2;
    private String part3;
    private Integer part4;
    private Integer part5;
    private BaseInfo baseInfo;


    public String getPart1() {
        return part1;
    }

    public void setPart1(String part1) {
        this.part1 = part1;
    }

    public String getPart2() {
        return part2;
    }

    public void setPart2(String part2) {
        this.part2 = part2;
    }

    public String getPart3() {
        return part3;
    }

    public void setPart3(String part3) {
        this.part3 = part3;
    }

    public Integer getPart4() {
        return part4;
    }

    public void setPart4(Integer part4) {
        this.part4 = part4;
    }

    public Integer getPart5() {
        return part5;
    }

    public void setPart5(Integer part5) {
        this.part5 = part5;
    }

    public BaseInfo getBaseInfo() {
        return baseInfo;
    }

    public void setBaseInfo(BaseInfo baseInfo) {
        this.baseInfo = baseInfo;
    }

    public Product4(String part1, String part2, String part3, Integer part4, Integer part5, BaseInfo baseInfo) {
        this.part1 = part1;
        this.part2 = part2;
        this.part3 = part3;
        this.part4 = part4;
        this.part5 = part5;
        this.baseInfo = baseInfo;
    }

    @Override
    public String toString() {
        return "["+super.hashCode()+"]Product4{" +
                "part1='" + part1 + '\'' +
                ", part2='" + part2 + '\'' +
                ", part3='" + part3 + '\'' +
                ", part4=" + part4 +
                ", part5=" + part5 +
                ", baseInfo=" + baseInfo +
                '}';
    }
//重写clone方法
    //将拷贝对象引用类型的值设置成被拷贝对象引用类型的克隆
    @Override
    protected Product4 clone() throws CloneNotSupportedException {
        Product4 clone = (Product4) super.clone();
        BaseInfo clone1 = this.baseInfo.clone();
        clone.setBaseInfo(clone1);
        return clone;
    }
}
